//
// Created by jiangpenghui on 2023/5/26.
//

#include "throw_noexcept.h"
#include <chrono>

void throw_run_error() noexcept(false) {
    //程序退出时
    atexit([] -> void {
        std::cout << "run lambda when program is error" << std::endl;
    });
    throw "abcd";
}

using namespace std::chrono;

void print_time() {
    auto now = time(nullptr);
    // Fri May 26 16:21:17 2023
    std::cout << ctime(&now) << std::endl;

    auto ltm = localtime(&now);

    char buffer[80];
    strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", ltm);

    std::cout << buffer << std::endl;


    std::cout << ltm->tm_hour << std::endl;


    time_point<system_clock> system_now = system_clock::now();

    // 时间戳
    auto timestamp = system_now.time_since_epoch().count();
    std::cout << timestamp << std::endl;

    // 10 秒
    seconds sec(10);

    // 3000 毫秒，使用double类型包装
    duration<double, std::milli> ms3k(3000);
    // 类型转换
    auto now_in_seconds = time_point_cast<seconds>(system_now);


}


void time_time() {
    time_t tm = time(nullptr);

    std::cout << "UNIX Time : " << tm << std::endl;

    const time_t *time_t_p = &tm;

    auto tm_p = gmtime(time_t_p);
    std::cout << "tm_p is hour " << tm_p->tm_hour << std::endl;

    // 时间是否相同
    std::cout << "localtime is hour " << localtime(&tm)->tm_hour << std::endl;


    duration<int, std::ratio<10>> tenths{5};
    auto count = tenths.count();
    std::cout << count << std::endl;

    std::cout << asctime(tm_p) << std::endl;

    char str_time [40];

    strftime(str_time,sizeof(str_time),"%Y-%m-%d %H:%M:%S",tm_p);

    std::cout << str_time << std::endl;
    seconds t(20);




}